package com.problem.leetcode;

/**
 * 问题1的简化版。思路就是设置两个指针，分别指向数组的头和尾，
 * @author didi
 * @DESCRIPTION
 * @create 2019-04-16 10:59
 **/

public class Problem167 {
    public int[] twoSum(int[] numbers, int target) {
        int[] result = new int[2];
        int begin = 0,end = numbers.length -1;

        while (begin < end) {
            if(numbers[begin] + numbers[end] > target) {
                --end;
            }else if (numbers[begin] + numbers[end] < target) {
                ++begin;
            }else {
                result[0] = begin+1;
                result[1] = end+1;
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem167 problem167 = new Problem167();
        int[] result = problem167.twoSum(new int[]{2, 7, 11, 15}, 9);
        System.out.println(result[0]);
        System.out.println(result[1]);
    }
}
